package ui;

import android.content.Context;

import com.wang.avi.AVLoadingIndicatorView;
import com.wang.avi.Indicator;

import java.util.WeakHashMap;

/**
 * Created by wzp on 2019/2/25 0025.
 * <p>
 * 描述  ：提前做个缓存
 */

public  final class LoaderCreator {


    private static final WeakHashMap<String , Indicator> LOAD_MAP = new WeakHashMap<>();

    static AVLoadingIndicatorView create (String  type , Context context){
        AVLoadingIndicatorView avLoadingIndicatorView =
                 new AVLoadingIndicatorView(context);
        if (LOAD_MAP.get(type) == null){
            final Indicator indicator = getindicator(type);
            LOAD_MAP.put(type,indicator);
        }
        avLoadingIndicatorView.setIndicator(LOAD_MAP.get(type));
        return avLoadingIndicatorView;
    }



    private static Indicator getindicator(String name){
        if (name == null || name.isEmpty()){
             return null;
        }
         final  StringBuilder drawableClassName = new StringBuilder();
          //说明是不是包名传所以要拼接
         if (!name.contains(".")){
             final String defaultPackageName = AVLoadingIndicatorView.class.getPackage().getName();
             drawableClassName.append(defaultPackageName)
             .append(".indicators")
             .append(".");
         }
         drawableClassName.append(name);
        try {
            final Class<?> drawableClass = Class.forName(drawableClassName.toString());
            return (Indicator) drawableClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



}
